package 设计模式.观察者模式.钟表;

import java.util.ArrayList;
import java.util.List;

/**
 * 闹钟
 *
 * @author 孔令富  2021/10/21
 */
public class Clock {

    // 一对多，需要接到通知的对象
    private List<AbstractInfo> infos = new ArrayList<>();

    public static void main(String[] args) {
        Clock clock = new Clock();
        AbstractInfo eat = new EatInfo();
        AbstractInfo sp = new SleepInfo();
        clock.addInfo(eat);
        clock.addInfo(sp);
        clock.say();
    }

    public void say() {
        System.out.println("上班了!");
        //通知
        update();
    }

    // 通知
    private void update() {
        for (AbstractInfo info : infos) {
            info.message();
        }
    }

    public void addInfo(AbstractInfo info) {
        infos.add(info);
    }
}
